package com.lkk.cg;

import com.lkk.cg.domain.*;
import com.lkk.cg.service.ArticleService;
import com.lkk.cg.service.PrivilegeService;
import com.lkk.cg.service.RoleService;
import com.lkk.cg.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.StrutsSpringTestCase;

import java.util.*;

/**
 * Created by lkk on 2014/12/2.
 */
public class ActionBaseTestCase extends StrutsSpringTestCase {
    protected Log log = LogFactory.getLog(getClass());

    //====================Get Service Bean====================

    protected <T> T getServiceBean(Class<T> clazz) {
        Object serviceBean = applicationContext.getBean(clazz);
        return (T) serviceBean;
    }

    protected UserService getUserService() {
        return getServiceBean(UserService.class);
    }

    protected RoleService getRoleService() {
        return getServiceBean(RoleService.class);
    }

    protected PrivilegeService getPrivilegeService() {
        return getServiceBean(PrivilegeService.class);
    }

    protected ArticleService getArticleService() {
        return getServiceBean(ArticleService.class);
    }

    //====================Sample Data====================

    protected User getSampleUser() {
        return getSampleUser(1L, "bob", "Bob", "sample@gmail.com", "password", "dev");
    }

    @Deprecated
    protected User getSampleUser(Long id, String name, String loginName, String email) {
        return getSampleUser(id, name, loginName, email, "password", "dev");
    }

    protected User getSampleUser(Long id, String name, String loginName, String email, String password, String department) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setLoginName(loginName);
        user.setEmail(email);
        user.setDepartment(department);
        user.setPassword(DigestUtils.md5Hex(password));
        return user;
    }

    protected Role getSampleRole(Long id, String name, String desc) {
        Role role = new Role();
        role.setId(id);
        role.setName(name);
        role.setDescription(desc);
        return role;
    }

    protected Role getSampleRole() {
        return getSampleRole(1L, "admin", "admin");
    }

    protected List<Role> getSampleRoleList() {
        List<Role> roles = new ArrayList<Role>();
        roles.add(getSampleRole(1L, "admin", "admin"));
        roles.add(getSampleRole(2L, "user", "user"));
        return roles;
    }

    protected List<Privilege> getSamplePrivilegeList() {
        Privilege privilege1 = new Privilege("p1", "u1", 1);
        privilege1.setId(1L);
        Privilege privilege2 = new Privilege("p2", "u2", 2);
        privilege2.setId(2L);
        List<Privilege> privilegeList = new ArrayList<Privilege>();
        privilegeList.add(privilege1);
        privilegeList.add(privilege2);
        return privilegeList;
    }

    protected Article getSampleArticle() {

        Article article = new Article();
        article.setId(1L);
        article.setTitle("MergeSort");
        article.setDescription("MergeSort is a sort method");

        //set author
        User author = getSampleUser();
        article.setAuthor(author);

        //set replies
        Reply reply = new Reply();
        Set<Reply> replies = new HashSet<Reply>();
        replies.add(reply);
        article.setReplies(replies);

        return article;
    }

    //====================Assist Verify Method====================

    protected void verifyActionErrors(int count, ActionProxy proxy) {
        assertEquals(count, ((ActionSupport) proxy.getAction()).getActionErrors().size());
    }

    protected void verifyActionMsgs(int count, ActionProxy proxy) {
        assertEquals(count, ((ActionSupport) proxy.getAction()).getActionMessages().size());
    }

    protected void verifyTopOfValueStack(Object expected) {
        assertEquals(expected, ActionContext.getContext().getValueStack().peek());
    }

    protected void verifyValueInContext(String key, Object expected) {
        assertEquals(expected, ActionContext.getContext().get(key));
    }
}
